<template>
  <div class="model-container">
    <div class="listName">评论管理</div>
    <!--搜索-->
    <el-card shadow="never">
      <div slot="header" class="clearfix">
        <div class="btns-wrapper-right">
          <el-button size="small" type="primary" plain @click="goBack">返回</el-button>
        </div>
      </div>
    </el-card>
    <!--表格-->
    <el-card style="margin-top: 10px">
      <div class="table-wrapper">
        <el-table v-loading="!dataOption" :stripe="true" :data="dataOption" border empty-text="暂无数据">

          <el-table-column align="center" prop="nick" label="评论人"/>

          <el-table-column align="center" prop="content" label="评论内容"/>

          <el-table-column align="center" prop="replyUserName" label="艾特人"/>

          <el-table-column align="center" label="状态">
            <template slot-scope="scope">{{ scope.row.isHidden === 0?'显示':'隐藏' }}</template>
          </el-table-column>

          <el-table-column align="center" label="操作" width="200px">
            <template slot-scope="scope">

              <el-button title="编辑" type="primary" circle icon="el-icon-edit" class="table-icon" @click="edit(scope.row)"/>

              <el-button v-if="scope.row.isHidden === 1" title="显示" type="primary" circle class="table-icon " @click="hidden(scope.row)"><i class="iconfont icon-chakan2"/></el-button>

              <el-button v-else title="隐藏" type="primary" circle class="table-icon " @click="hidden(scope.row)"><i class="iconfont icon-lahei"/></el-button>

              <el-button title="删除" type="warning" circle icon="el-icon-delete" class="table-icon" @click="del(scope.row)"/>

            </template>
          </el-table-column>

        </el-table>

        <!--分页-->
        <el-pagination
          :current-page="pageNo"
          :page-sizes="[10, 20, 30]"
          :page-size="pageSize"
          :total="rowCount"
          background
          layout="total, sizes, prev, pager, next, jumper"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"/>
      </div>
    </el-card>

    <!--弹框-->
    <el-dialog
      :visible.sync="dialogState"
      :close-on-click-modal="false"
      :before-close="handleClose"
      title="修改内容"
      top="5vh"
      width="40%">
      <el-form ref="dialogBindData" :model="dialogBindData" :rules="rules" label-width="30%" status-icon class="demo-ruleForm">
        <div class="formBody">
          <el-form-item label="评论内容" prop="content" class="three">
            <el-input v-model="dialogBindData.content" autosize type="textarea" placeholder="请输入评论内容"/>
          </el-form-item>
        </div>
        <div class="btns-wrapper">
          <el-button size="small" @click="handleClose">取 消</el-button>
          <el-button :loading="buttonState" type="primary" size="small" @click="submmitFun">保 存</el-button>
        </div>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
import * as fishing from '../../api/fishing'
export default {
  data() {
    return {
      pageNo: 1,
      pageSize: 10,
      rowCount: null, // 总条数
      // 弹框
      dialogState: false,
      // 按钮状态
      buttonState: false,
      // 列表数据
      dataOption: null,
      // 提交内容
      dialogBindData: {
        id: '',
        pid: '',
        content: ''
      },
      rules: {
        content: [
          { required: true, message: '请输入内容', trigger: 'blur' }
        ]
      }
    }
  },
  beforeMount() {
    this.dialogBindData.pid = this.$route.params.id // 编辑数据
    this.getList()
  },
  methods: {
    seachFun() {

    },
    getList() {
      const param = {
        current: this.pageNo,
        size: this.pageSize,
        pid: this.dialogBindData.pid
      }
      fishing.getFishoneList(param).then(res => {
        if (res.data.code !== 1) {
          this.$message.error(res.data.msg)
          return false
        }
        if (res.data.data === 'null' || res.data.data === null) {
          return false
        }
        // 初始话表格数据
        this.dataOption = res.data.data.records
        // 初始化分页数据
        this.rowCount = res.data.data.total
        this.pageSize = res.data.data.size
      })
    },
    // 新增
    add() {
      this.dialogState = true
    },
    // 编辑
    edit(data) {
      this.dialogState = true
      this.dialogBindData.id = data.id
      this.reBindData(data)
    },
    // 提交
    submmitFun() {
      const param = {
        id: this.dialogBindData.id,
        content: this.dialogBindData.content
      }

      this.$refs.dialogBindData.validate((valid) => {
        if (valid) {
          fishing.editorFishoneList(param).then(res => {
            if (res.data.code !== 1) {
              this.$message({
                message: res.data.msg,
                type: 'warning'
              })
              this.buttonState = false
              return false
            }
            this.$message({
              message: '编辑成功',
              type: 'success'
            })
            this.buttonState = false
            this.getList()
            this.handleClose()
          })
        } else {
          return false
        }
      })
    },
    // 删除
    del(data) {
      this.$confirm('此操作将永久删除该数据, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        fishing.delFishoneList(data.id).then(res => {
          if (res.data.code === 1) {
            this.$message({
              message: '删除成功',
              type: 'success'
            })
            this.buttonState = false
            this.getList()
            this.handleClose()
          }
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    hidden(data) {
      this.$confirm('确认下步操作吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        const param = {
          id: data.id
        }
        if (data.isHidden === 0) {
          param.isHidden = 1
        } else {
          param.isHidden = 0
        }
        fishing.editorFishoneList(param).then(res => {
          if (res.data.code === 1) {
            this.$message({
              message: '编辑成功',
              type: 'success'
            })
            this.buttonState = false
            this.getList()
            this.handleClose()
          }
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    // 反显
    reBindData(data) {
      for (const key in this.dialogBindData) {
        this.dialogBindData[key] = data[key]
      }
    },
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`)
      this.pageSize = val
      this.getList()
    },
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`)
      this.pageNo = val
      this.getList()
    },
    handleClose() {
      for (const key in this.dialogBindData) {
        this.dialogBindData[key] = ''
      }
      this.dialogState = false
      this.$refs.dialogBindData.clearValidate()
    },
    goBack() {
      this.$router.go(-1)
    }
  }
}
</script>
<style type="text/scss" lang="scss" scoped>

</style>
